package ebookcleaner;

import java.io.File;
import java.io.FilenameFilter;
import tools.FileTools;
import tools.CompressTools;

public class ShelfExtractor{

	String rarBinaryFP, srcRootFP, destRootFP,leftOverFP;

	ShelfExtractor(String rarBinaryFP, String srcRootFP, String destRootFP,String leftOverFP) {
		this.rarBinaryFP = rarBinaryFP;
		this.srcRootFP = srcRootFP;
		this.destRootFP = destRootFP;
		this.leftOverFP = leftOverFP;

		File destFolder = new File(destRootFP);
		if (!destFolder.exists()) {
			destFolder.mkdir();
		}
	}

	/**
	 * Initiate variables from Properties
	 */
	public void process() {
		extractAndCopy(srcRootFP, destRootFP);
		FileTools.deleteAllEmptyFolder(srcRootFP);
		FileTools.deleteAllEmptyFolder(destRootFP);
		System.out.println("------------COMPLETED ReadyUnzipper----------");
	}

	private boolean extractAndCopy(String srcFP, String destFP) {

		File srcFile = new File(srcFP);
		File destFile = new File(destFP);

		if (srcFile.exists()) {
			String srcBaseFP = srcFile.getPath();

			String[] zipFP = getListOfZIPFiles(srcFile.getPath());
			if (zipFP.length >= 1) {
				String namedTempFP = destFP + "\\" + srcFile.getName();
				
				File namedTempFolder = new File(namedTempFP);
				if (!namedTempFolder.exists()){
					namedTempFolder.mkdir();
				}
				for (String each : zipFP) {
					CompressTools.unzip(srcBaseFP + "\\" + each, namedTempFP);
					File zipFile = new File(srcBaseFP + "\\" + each );
					zipFile.delete();

				}
				extractAndCopy(namedTempFP, namedTempFP);
			}

			String[] rarFP = getListOfRARFiles(srcFile.getPath());
			if (rarFP.length >= 1) {
				String namedTempFP = destFP + "\\" + srcFile.getName();
				File namedTempFolder = new File(namedTempFP);
				if (!namedTempFolder.exists()){
					namedTempFolder.mkdir();
				}
				for (String each : rarFP) {
					CompressTools.unrar(rarBinaryFP, "x", srcBaseFP + "\\" + each, namedTempFP);
					File rarFile = new File(srcBaseFP + "\\" + each );
					rarFile.delete();

				}
				extractAndCopy(namedTempFP, namedTempFP);
			}

			File[] files = srcFile.listFiles();
			for (File each : files) {
				if (each.isDirectory()) {
					extractAndCopy(each.getPath(), destFP+"\\"+each.getName());

				} else {

					String parentFolderName = FileTools.getParentFolderName(each.getPath());
					FILETYPE ft = FILETYPE.getFileType(FileTools.getFileExt(each.getName()));

					switch (ft) {
					case M4U:
					case WMV:
					case MP3:
					case AVI:
					case MP4:
						File newFolderFile = new File(destFP + "\\" + parentFolderName);

						if (!newFolderFile.exists())
							newFolderFile.mkdir();

						System.out.println(">> MP3: 	" + destFP + "\\" + parentFolderName + "\\" + each.getName());
						each.renameTo(new File(destFP + "\\" + parentFolderName + "\\" + each.getName()));
						break;

					case PDF:
					case HWP:
					case GUL:
					case XLS:
					case XLSX:
					case PPT:
					case PPTX:
					case DOC:
					case DOCX:
					case TXT:

						FileTools.moveFile(each.getPath(), destFP);
						
						System.out.println(">> SRC: 	" + each.getPath());
						System.out.println(">> DST: 	" + destFP);
						break;
					default:
						FileTools.moveFile(each.getPath(), leftOverFP);
						System.out.println(">> Moved: 	" + each.getPath());
						//System.out.println(">> Deleted: 	" + each.getPath());
						//each.delete();
						break;
					}
				}
			}
		}

		if (!srcFile.getParent().equalsIgnoreCase(srcRootFP))
			return (srcFile.delete());
		else
			return true;
	}

	private String[] getListOfZIPFiles(String aDirectory) {
		// our filename filter (filename pattern matcher)
		class OnlyUnderscoreFilter implements FilenameFilter {
			@Override
			public boolean accept(File dir, String s) {
				if (s.toLowerCase().endsWith(".zip"))
					return true;
				return false;
			}
		}
		return new java.io.File(aDirectory).list(new OnlyUnderscoreFilter());
	}

	private String[] getListOfRARFiles(String aDirectory) {
		// our filename filter (filename pattern matcher)
		class OnlyUnderscoreFilter implements FilenameFilter {
			@Override
			public boolean accept(File dir, String s) {
				if (s.toLowerCase().endsWith(".rar"))
					return true;
				return false;
			}
		}
		return new java.io.File(aDirectory).list(new OnlyUnderscoreFilter());
	}
}
